import urllib
import datetime
import xml.dom.minidom
from xml.dom.minidom import Node
import re

yesterday=datetime.date.today()-datetime.timedelta(days=1)
difference = datetime.timedelta(weeks=-4)
before=yesterday+difference

# Get a file-like object for the Python Web site's home page.
f = urllib.urlopen("http://feedburner.google.com/api/awareness/1.0/GetResyndicationData?uri=myoutsourcedbrain&dates="+before.isoformat() + "," + yesterday.isoformat())
# Read from the object, storing the page's contents in 's'.
s = f.read()
f.close()

doc=xml.dom.minidom.parseString(s)
visited=[]

def handleEntry(entry):
  #print "entry"
  for node in entry.getElementsByTagName("item"):
    #print node
    title = node.getAttribute("title")
    url = node.getAttribute("url")
    itemviews = node.getAttribute("itemviews")
    visited.append((itemviews,url,title))

handleEntry(doc)
visited.sort()
visited.reverse()


# ten most popular posts
for post in visited[0:10]:
  print('document.write(\'<li>\');')
  print('document.write(\'<a href="' + post[1] + '">\');')
  print('document.write(\'' + re.escape(post[2]) + '</a>\');')
  print('document.write(\'</li>\');')

